package com.edu.compile.util;

import com.edu.compile.template.model.SubPath;
import com.edu.compile.template.model.UrlPath;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yanzhao
 * @date 2021/10/30 15:40
 * @since 1.0.0
 */
public class ParamUtils {

    public static final String PATH_PARAM_START = "{";

    public static final String PATH_PARAM_END = "}";

    public static final String URL_SEPARATOR = "/";


    public static void main(String[] args) {
        String url = "s/a/{b}/c/{d}";
        show(parsePathParams(url));
        url = "/s/a/{b}/{c}/d/";
        show(parsePathParams(url));
        url = "{d}/s/a/{b}/{c}/d/";
        show(parsePathParams(url));
        url = "/{d}/s/a/{b}/{c}/d/";
        show(parsePathParams(url));

    }

    public static String deleteWhitespace(String url) {
        return StringUtils.deleteWhitespace(url);
    }

    public static boolean isUrlSeparatorStart(String url) {
        return url.startsWith(URL_SEPARATOR);
    }

    public static boolean isUrlSeparatorEnd(String url) {
        return url.endsWith(URL_SEPARATOR);
    }

    public static void show(UrlPath urlPath) {
        StringBuilder buffer = new StringBuilder();

        List<SubPath> subPaths = urlPath.getSubPaths();
        buffer.append(urlPath.getHasHeadSeparator() ? "/" : "");
        List<String> paths = new ArrayList<>();
        subPaths.forEach((path) -> {
            paths.add(path.getIsParam() ? "+" + path.getPath() + "+" : path.getPath());
        });
        buffer.append(StringUtils.join(paths, "/"));
        buffer.append(urlPath.getHasTailSeparator() ? "/" : "");
        if (buffer.indexOf("+") == 0) {
            buffer.deleteCharAt(0);
        }
        if (buffer.lastIndexOf("+") == buffer.length() - 1) {
            buffer.deleteCharAt(buffer.length() - 1);
        }
        System.out.println(buffer.toString());
    }

    public static UrlPath parsePathParams(String url) {

        if (StringUtils.isBlank(url)) {
            return null;
        }

        url = deleteWhitespace(url);

        UrlPath urlPath = new UrlPath();
        List<SubPath> subPathList = new ArrayList<>();

        urlPath.setHasHeadSeparator(isUrlSeparatorStart((url)));
        urlPath.setHasTailSeparator(isUrlSeparatorEnd((url)));
        urlPath.setSubPaths(subPathList);
        String[] subPaths = url.split(URL_SEPARATOR);

        for (int i = 0; i < subPaths.length; i++) {
            String subPath = subPaths[i];
            if (StringUtils.isBlank(subPath)) {
                continue;
            }
            SubPath paramPath = new SubPath();
            if (subPath.startsWith(PATH_PARAM_START) && subPath.endsWith(PATH_PARAM_END)) {
                paramPath.setIsParam(true);
                paramPath.setPath(subPath.substring(1, subPath.length() - 1));
            } else {
                paramPath.setIsParam(false);
                paramPath.setPath(subPath);
            }
            subPathList.add(paramPath);
        }

        return urlPath.merge();
    }
}
